package com.zcn.io.web.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcn.io.common.entity.PayDetail;
import com.zcn.io.common.util.DateUtil;
import com.zcn.io.common.util.PayCalculateUtils;
import com.zcn.io.common.vo.OrderMainForTrusteeshipVo;
import com.zcn.io.common.vo.OrderMainPayDetailVo;
import com.zcn.io.common.vo.PayTotalVo;
import com.zcn.io.web.api.query.OrderMainQuery;
import com.zcn.io.web.api.vo.OrderMainVo;
import com.zcn.io.web.api.vo.PhoneCardVo;
import com.zcn.io.web.mapper.OrderMapper;
import com.zcn.io.common.entity.OrderMain;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @desc:
 * @author: 202438 邱建伟
 * @date: 2024/9/22 10:39
 * @Copyright (c) 2024, 江苏金恒信息科技股份有限公司
 */
@Service
@RequiredArgsConstructor
public class OrderService {
    private final OrderMapper orderMapper;
    private final PayDetailService payDetailService;
    private final OrderTransferPhoneRecordService orderTransferPhoneRecordService;

    public void list(Page<OrderMain> page, OrderMainQuery orderMain) {

        this.orderMapper.listMain(page, orderMain);
    }

    public void save(OrderMain orderMain) {
        this.orderMapper.insert(orderMain);
        orderTransferPhoneRecordService.save(orderMain);
    }

    public OrderMainVo getById(String id) {
        return orderMapper.getById(id);
    }

    public void updateById(OrderMain orderMain) {
        OrderMain currentMain = orderMapper.selectById(orderMain.getId());
        if (!StrUtil.equals(orderMain.getTransferPhone(), currentMain.getTransferPhone())) {
            //更新手机号变更记录
            orderTransferPhoneRecordService.save(orderMain);
        }
        //处理退单状态的时间
        if (StrUtil.equals("已取消",orderMain.getOrderStatus())||StrUtil.equals("暂停(有群)",orderMain.getOrderStatus())) {
            orderMain.setRefundTime(DateUtil.getDefaultDate());
        }
        orderMapper.updateById(orderMain);
    }

    public void deleteById(String id) {
        orderMapper.deleteById(id);
    }

    public void setOrderTotalPay(OrderMain orderMain) {
        LambdaUpdateWrapper<OrderMain> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(OrderMain::getId, orderMain.getId());
        updateWrapper.set(OrderMain::getOrderAmount, orderMain.getOrderAmount());
        this.orderMapper.update(null, updateWrapper);
    }

    public OrderMainPayDetailVo getDetailById(String orderId) {
        OrderMain orderMain = this.getById(orderId);
        List<PayDetail> payDetails = payDetailService.listAllByOrderId(orderId);
        OrderMainPayDetailVo orderMainPayDetailVo = BeanUtil.copyProperties(orderMain, OrderMainPayDetailVo.class);
        orderMainPayDetailVo.setPayDetailList(payDetails);
        PayTotalVo payTotalVo = PayCalculateUtils.getPayTotalVo(orderMain, payDetails);
        orderMainPayDetailVo.setPayPend(payTotalVo.getRestTotal());
        orderMainPayDetailVo.setProfit(payTotalVo.getProfit());
        return orderMainPayDetailVo;
    }

    public IPage<OrderMainForTrusteeshipVo> listOrderInfoForTrusteeship(Page<OrderMainForTrusteeshipVo> page, OrderMainQuery orderMain) {
        return this.orderMapper.listOrderInfoForTrusteeship(page, orderMain);
    }

    public void saveBatch(List<OrderMain> saveDataList) {
        for (OrderMain orderMain : saveDataList) {
            this.orderMapper.insert(orderMain);
        }
    }

    public void updateNativePlaceHandle(OrderMain orderMain) {
        this.orderMapper.updateNativePlaceHandle(orderMain);
    }

    public void updateNativePlaceAndOnlineInfoHandle(OrderMain orderMain) {
        this.orderMapper.updateNativePlaceAndOnlineInfoHandle(orderMain);
    }

    public void getInvalidOrderMain(Page<OrderMain> page, OrderMainQuery orderMain) {
        this.orderMapper.getInvalidOrderMain(page, orderMain);
    }

    public void getReminderTimeList(Page<OrderMainVo> page, OrderMainQuery orderMain) {
        orderMain.setCurrentDateTime(DateUtil.getDefaultDate());
        this.orderMapper.getReminderTimeList(page, orderMain);
    }

    public void saveCreditCardInfo(OrderMain orderMain) {
        this.orderMapper.saveCreditCardInfo(orderMain);
    }

    public void updateOnlineLendingInfo(OrderMain orderMain) {
        this.orderMapper.updateOnlineLendingInfo(orderMain);
    }

    public void updateReminderMemo(OrderMain orderMain) {
        this.orderMapper.updateReminderMemo(orderMain);
    }

    public List<OrderMainPayDetailVo> getUseOrderList(OrderMainQuery orderMain) {
        return this.orderMapper.getUseOrderList(orderMain);
    }

    public void setCancelMemo(OrderMain orderMain) {
        this.orderMapper.setCancelMemo(orderMain);
    }

}
